package xhwl.retrofitrx;

import com.trello.rxlifecycle.android.ActivityEvent;
import com.trello.rxlifecycle.android.FragmentEvent;
import com.trello.rxlifecycle.components.RxActivity;
import com.trello.rxlifecycle.components.support.RxFragment;
import com.trello.rxlifecycle.components.support.RxFragmentActivity;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2017/1/12.
 */

public class HttpManager {
    private static volatile HttpManager instance;

    private HttpManager() {
    }

    public static HttpManager getInstance() {
        if (instance == null) {
            synchronized (HttpManager.class) {
                if (instance == null) {
                    instance = new HttpManager();
                }
            }
        }
        return instance;
    }


    public void dealHttp(RxActivity context, BaseApi api, OnNextListener listener) {
        /*rx处理*/
        ProgressSubscriber subscriber = new ProgressSubscriber(context, api, listener);
        rx.Observable observable = api.getObservable()
                /*失败后的retry配置*/
                //总共重试3次，重试间隔3000毫秒
                .retryWhen(new RetryWhenNetworkException(api.getRetryCount(),api.getRetryDelay()))
                /*异常处理*/
                .onErrorResumeNext(funcException)
                /*生命周期管理*/
//                .compose(context.bindToLifecycle())
                //Note:手动设置在activity onDestroy的时候取消订阅
                .compose(context.bindUntilEvent(ActivityEvent.DESTROY))
                /*http请求线程*/
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                /*回调线程*/
                .observeOn(AndroidSchedulers.mainThread());

        /*数据回调*/
        observable.subscribe(subscriber);
    }
    public void dealHttp(RxFragmentActivity context, BaseApi api, OnNextListener listener) {
        /*rx处理*/
        ProgressSubscriber subscriber = new ProgressSubscriber(context, api, listener);
        rx.Observable observable = api.getObservable()
                /*失败后的retry配置*/
                //总共重试3次，重试间隔3000毫秒
                .retryWhen(new RetryWhenNetworkException(api.getRetryCount(),api.getRetryDelay()))
                /*异常处理*/
                .onErrorResumeNext(funcException)
                /*生命周期管理*/
//                .compose(context.bindToLifecycle())
                //Note:手动设置在activity onDestroy的时候取消订阅
                .compose(context.bindUntilEvent(ActivityEvent.DESTROY))
                /*http请求线程*/
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                /*回调线程*/
                .observeOn(AndroidSchedulers.mainThread());

        /*数据回调*/
        observable.subscribe(subscriber);
    }
    public void dealHttp(RxFragment fragment, BaseApi api, OnNextListener listener) {
        /*rx处理*/
        ProgressSubscriber subscriber = new ProgressSubscriber(fragment, api, listener);
        rx.Observable observable = api.getObservable()
                /*失败后的retry配置*/
                //总共重试3次，重试间隔3000毫秒
                .retryWhen(new RetryWhenNetworkException(api.getRetryCount(),api.getRetryDelay()))
                /*异常处理*/
                .onErrorResumeNext(funcException)
                /*生命周期管理*/
//                .compose(context.bindToLifecycle())
                //Note:手动设置在activity onDestroy的时候取消订阅
                .compose(fragment.bindUntilEvent(FragmentEvent.DESTROY))
                /*http请求线程*/
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                /*回调线程*/
                .observeOn(AndroidSchedulers.mainThread());

        /*数据回调*/
        observable.subscribe(subscriber);
    }

    /**
     * 异常处理
     */
    Func1 funcException = new Func1<Throwable, rx.Observable>() {
        @Override
        public rx.Observable call(Throwable throwable) {
            return rx.Observable.error(ExceptionFactory.analysisExcetpion(throwable));
        }
    };
}
